package com.fenghuajueli.lib_pictureselect.activity;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.viewpager2.widget.ViewPager2;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.fenghuajueli.lib_pictureselect.R;
import com.fenghuajueli.lib_pictureselect.adapter.PreviewMaterialAdapter;
import com.fenghuajueli.lib_pictureselect.config.PhotoConfig;
import com.fenghuajueli.lib_pictureselect.entity.SelectMediaEntity;
import com.fenghuajueli.lib_pictureselect.utils.MediaSelectDataUtils;

import java.util.List;


/**
 * 素材预览页
 */
public class MaterialPreviewActivity extends BaseSelectActivity {

    private PreviewMaterialAdapter previewMaterialAdapter;

    private ViewPager2 materialPreviewContainer;
    private ConstraintLayout selectContainer;
    private CheckBox checkbox;
    private TextView tvPosition;
    private ImageView ivClosePreview;
    private int type = -1;// 0 全部分类 1 视频 2 图片
    private int currentPosition = 0;
    private List<SelectMediaEntity> currentDataList;

    public static ChoiceMaterialListener choiceMaterialListener;

    public static void start(Context context, int type, int position) {
        Intent intent = new Intent(context, MaterialPreviewActivity.class);
        intent.putExtra("type", type);
        intent.putExtra("position", position);
        context.startActivity(intent);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_material_preview);
        materialPreviewContainer = findViewById(R.id.materialPreviewContainer);
        selectContainer = findViewById(R.id.selectContainer);
        checkbox = findViewById(R.id.checkbox);
        tvPosition = findViewById(R.id.tvPosition);
        ivClosePreview = findViewById(R.id.ivClosePreview);
        type = getIntent().getIntExtra("type", -1);
        currentPosition = getIntent().getIntExtra("position", 0);
        if (type == 0) {
            initAdapter(MediaSelectDataUtils.allSelectMediaEntityList);
        } else if (type == 1) {
            initAdapter(MediaSelectDataUtils.videoSelectMediaEntityList);
        } else if (type == 2) {
            initAdapter(MediaSelectDataUtils.imageSelectMediaEntityList);
        } else {
            finish();
        }
        ivClosePreview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    private void initAdapter(List<SelectMediaEntity> selectMediaEntities) {
        if (selectMediaEntities != null && selectMediaEntities.size() > 0) {
            currentDataList = selectMediaEntities;
            previewMaterialAdapter = new PreviewMaterialAdapter(this, selectMediaEntities);
            materialPreviewContainer.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
            materialPreviewContainer.setAdapter(previewMaterialAdapter);
            materialPreviewContainer.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                    super.onPageScrolled(position, positionOffset, positionOffsetPixels);
                }

                @Override
                public void onPageSelected(int position) {
                    super.onPageSelected(position);
                    currentPosition = position;
                    tvPosition.setText(position + 1 + "/" + selectMediaEntities.size());
                    checkIsSelected(position);
                    materialPreviewContainer.post(new Runnable() {
                        @Override
                        public void run() {
                            if (position - 1 >= 0) {
                                previewMaterialAdapter.notifyItemChanged(position - 1);
                            }
                            if (position + 1 < selectMediaEntities.size()) {
                                previewMaterialAdapter.notifyItemChanged(position + 1);
                            }
                        }
                    });
                }

                @Override
                public void onPageScrollStateChanged(int state) {
                    super.onPageScrollStateChanged(state);
                    previewMaterialAdapter.stopAllPlay();
                }
            });
            materialPreviewContainer.setCurrentItem(currentPosition, false);
            tvPosition.setText(currentPosition + 1 + "/" + selectMediaEntities.size());
            selectContainer.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (currentPosition >= 0 && currentPosition < currentDataList.size()) {
                        SelectMediaEntity selectMediaEntity = currentDataList.get(currentPosition);
                        if (!selectMediaEntity.isChecked()) {
                            if (MediaSelectDataUtils.alreadySelectMediaEntityList.size() == PhotoConfig.getInstance().getMaxCount()) {
                                ToastUtils.showShort("已达到最大选择数量");
                                return;
                            }
                            selectMediaEntity.setChecked(true);
                            checkbox.setChecked(true);
                            MediaSelectDataUtils.alreadySelectMediaEntityList.add(selectMediaEntity);
                        } else {
                            selectMediaEntity.setChecked(false);
                            checkbox.setChecked(false);
                            MediaSelectDataUtils.alreadySelectMediaEntityList.remove(selectMediaEntity);
                        }
                    }
                }
            });
        } else {
            finish();
        }
    }


    private void checkIsSelected(int position) {
        if (position >= 0 && position < currentDataList.size()) {
            SelectMediaEntity selectMediaEntity = currentDataList.get(position);
            checkbox.setChecked(selectMediaEntity.isChecked());
            if (choiceMaterialListener != null) {
                choiceMaterialListener.onChoiceChange();
            }
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (previewMaterialAdapter != null) {
            previewMaterialAdapter.stopAllPlay();
        }
        choiceMaterialListener = null;
    }

    /**
     * 回调出去让主界面刷新
     */
    public interface ChoiceMaterialListener {
        void onChoiceChange();
    }
}